// src/com/company/ems/controller/EmployeeServlet.java
package com.company.ems.controller;

import com.company.ems.dao.EmployeeDAO;
import com.company.ems.model.Employee;
import com.company.ems.model.User;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.List;

public class EmployeeServlet extends HttpServlet {
    private EmployeeDAO employeeDAO;

    @Override
    public void init() throws ServletException {
        employeeDAO = new EmployeeDAO();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if (action == null) {
            action = "list";
        }

        switch (action) {
            case "new":
                showNewForm(request, response);
                break;
            case "edit":
                showEditForm(request, response);
                break;
            case "delete":
                deleteEmployee(request, response);
                break;
            default:
                listEmployees(request, response);
                break;
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if ("insert".equals(action)) {
            insertEmployee(request, response);
        } else if ("update".equals(action)) {
            updateEmployee(request, response);
        }
    }

    private void listEmployees(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Employee> employees = employeeDAO.findAll();
        request.setAttribute("employees", employees);
        request.getRequestDispatcher("/employee/list.jsp").forward(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/employee/add.jsp").forward(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Employee employee = employeeDAO.findById(id);
        request.setAttribute("employee", employee);
        request.getRequestDispatcher("/employee/edit.jsp").forward(request, response);
    }

    private void insertEmployee(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String name = request.getParameter("name");
        String department = request.getParameter("department");
        String position = request.getParameter("position");
        BigDecimal salary = new BigDecimal(request.getParameter("salary"));
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        Date hireDate = Date.valueOf(request.getParameter("hire_date"));

        Employee employee = new Employee();
        employee.setName(name);
        employee.setDepartment(department);
        employee.setPosition(position);
        employee.setSalary(salary);
        employee.setEmail(email);
        employee.setPhone(phone);
        employee.setHireDate(hireDate);
        employee.setCreatedBy(user.getId());

        employeeDAO.save(employee);
        response.sendRedirect("employee");
    }

    private void updateEmployee(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String department = request.getParameter("department");
        String position = request.getParameter("position");
        BigDecimal salary = new BigDecimal(request.getParameter("salary"));
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        Date hireDate = Date.valueOf(request.getParameter("hire_date"));

        Employee employee = new Employee();
        employee.setId(id);
        employee.setName(name);
        employee.setDepartment(department);
        employee.setPosition(position);
        employee.setSalary(salary);
        employee.setEmail(email);
        employee.setPhone(phone);
        employee.setHireDate(hireDate);

        employeeDAO.update(employee);
        response.sendRedirect("employee");
    }

    private void deleteEmployee(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        employeeDAO.delete(id);
        response.sendRedirect("employee");
    }
}